package com.micro.service.gateway.config.oauth2;

import com.micro.service.gateway.web.filter.CommonFilter;
import com.micro.service.gateway.web.filter.TPSFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @desc web 配置
 */
@Configuration
@Slf4j
public class WebConfiguration {


    @Value("${application.securityUrl}")
    private String     secureUrl;

    @Value("${application.rate}")
    private String     rate;



    /**
     * @param tpsFilter
     * 限流
     */
    @Bean
    FilterRegistrationBean limitFilterRegistration(TPSFilter tpsFilter){
        tpsFilter.setRate(Integer.valueOf(rate));
        log.info("服务端限流策略，允许每秒{}次请求",rate);
    	FilterRegistrationBean registration = new FilterRegistrationBean(tpsFilter);
    	registration.addUrlPatterns("/*");
    	return registration;
    }

    /**
     * @param commonFilter
     * 拦截所有请求
     */
    @Bean
    FilterRegistrationBean commonFilterRegistration(CommonFilter commonFilter) {
    	FilterRegistrationBean registration = new FilterRegistrationBean(commonFilter);
    	registration.addUrlPatterns("/*");
    	return registration;
    }

    
    /**
     * @param corsFilter
     * 支持跨域
     */
    /*@Bean
    FilterRegistrationBean corsFilterRegistration(CorsFilter corsFilter) {
    	FilterRegistrationBean registration = new FilterRegistrationBean(corsFilter);
    	registration.addUrlPatterns("/*");
    	return registration;
    }*/


    /*@Bean
    FilterRegistrationBean secureFilterRegistration() throws ServletException {
        SecureFilter secureFilter = new SecureFilter();
        FilterRegistrationBean registration = new FilterRegistrationBean(secureFilter);
        String[] urls = secureUrl.split(",");
        for (String url : urls) {
            registration.addUrlPatterns(url);
        }
        return registration;
    }*/
}
